const StringAlgorithms = require("../template")

function testPalindrome() {
  const stringAlgo = new StringAlgorithms()

  console.log(
    "1. 回文字符串检测测试:",
    (() => {
      const tests = [
        { input: "racecar", expected: true },
        { input: "hello", expected: false },
        { input: "a", expected: true },
        { input: "", expected: true },
        { input: "abba", expected: true },
        { input: "abcba", expected: true },
        { input: "abccba", expected: true },
        { input: "abcdcba", expected: true }
      ]
      return tests.every(test => stringAlgo.isPalindrome(test.input) === test.expected)
    })()
  )

  console.log(
    "2. 最长回文子串测试:",
    (() => {
      const tests = [
        { input: "babad", expected: "bab" },
        { input: "cbbd", expected: "bb" },
        { input: "a", expected: "a" },
        { input: "ac", expected: "a" },
        { input: "racecar", expected: "racecar" },
        { input: "abcdef", expected: "a" }
      ]
      return tests.every(test => stringAlgo.longestPalindrome(test.input) === test.expected)
    })()
  )

  console.log(
    "3. 回文子串数量统计测试:",
    (() => {
      const tests = [
        { input: "abc", expected: 3 },
        { input: "aaa", expected: 6 },
        { input: "a", expected: 1 },
        { input: "", expected: 0 },
        { input: "racecar", expected: 10 },
        { input: "abccba", expected: 9 }
      ]
      return tests.every(test => stringAlgo.countPalindromes(test.input) === test.expected)
    })()
  )

  console.log(
    "4. 回文分割测试:",
    (() => {
      const tests = [
        { input: "aab", expected: [["a", "a", "b"], ["aa", "b"]] },
        { input: "a", expected: [["a"]] },
        { input: "racecar", expected: [["racecar"], ["r", "a", "c", "e", "c", "a", "r"]] },
        { input: "", expected: [[]] }
      ]
      return tests.every(test => {
        const result = stringAlgo.palindromePartition(test.input)
        return JSON.stringify(result.sort()) === JSON.stringify(test.expected.sort())
      })
    })()
  )

  console.log(
    "5. 回文验证忽略大小写测试:",
    (() => {
      const tests = [
        { input: "RaceCar", expected: true },
        { input: "Hello", expected: false },
        { input: "A", expected: true },
        { input: "AbBa", expected: true },
        { input: "AbCbA", expected: true }
      ]
      return tests.every(test => stringAlgo.isPalindromeIgnoreCase(test.input) === test.expected)
    })()
  )

  console.log(
    "6. 回文验证忽略非字母数字测试:",
    (() => {
      const tests = [
        { input: "A man, a plan, a canal: Panama", expected: true },
        { input: "race a car", expected: false },
        { input: "Was it a car or a cat I saw?", expected: true },
        { input: "No 'x' in Nixon", expected: true },
        { input: "Madam, I'm Adam", expected: true }
      ]
      return tests.every(test => stringAlgo.isPalindromeIgnoreNonAlphanumeric(test.input) === test.expected)
    })()
  )

  console.log(
    "7. this上下文回文测试:",
    (() => {
      const palindromeObj = {
        multiplier: 2,
        processPalindrome: function(str) {
          const stringAlgo = new StringAlgorithms()
          const isPal = stringAlgo.isPalindrome(str)
          return isPal ? this.multiplier : 0
        }
      }
      return palindromeObj.processPalindrome("racecar") === 2 && 
             palindromeObj.processPalindrome("hello") === 0
    })()
  )

  console.log(
    "8. 复杂对象this绑定回文测试:",
    (() => {
      const palindromeConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(str) {
          const stringAlgo = new StringAlgorithms()
          const longestPal = stringAlgo.longestPalindrome(str)
          return longestPal.length > this.config.threshold
        }
      }
      return palindromeConfigObj.processWithConfig("racecar") === true &&
             palindromeConfigObj.processWithConfig("aba") === false
    })()
  )

  console.log(
    "9. 回文算法一致性测试:",
    (() => {
      const testString = "racecar"
      const isPal = stringAlgo.isPalindrome(testString)
      const longestPal = stringAlgo.longestPalindrome(testString)
      const count = stringAlgo.countPalindromes(testString)
      return isPal === true && longestPal === "racecar" && count > 0
    })()
  )

  console.log(
    "10. 回文算法边界测试:",
    (() => {
      const emptyResult = stringAlgo.isPalindrome("") === true &&
                         stringAlgo.longestPalindrome("") === "" &&
                         stringAlgo.countPalindromes("") === 0
      
      const singleCharResult = stringAlgo.isPalindrome("a") === true &&
                              stringAlgo.longestPalindrome("a") === "a" &&
                              stringAlgo.countPalindromes("a") === 1
      
      return emptyResult && singleCharResult
    })()
  )

  console.log(
    "11. 回文算法性能测试:",
    (() => {
      const longString = "a".repeat(1000) + "b".repeat(1000) + "a".repeat(1000)
      const start = performance.now()
      stringAlgo.isPalindrome(longString)
      stringAlgo.longestPalindrome(longString)
      stringAlgo.countPalindromes(longString)
      const end = performance.now()
      return (end - start) < 1000 // 性能测试应该在1秒内完成
    })()
  )

  console.log(
    "12. 回文算法复杂测试:",
    (() => {
      const complexString = "A man, a plan, a canal: Panama"
      const isPalIgnoreCase = stringAlgo.isPalindromeIgnoreCase(complexString)
      const isPalIgnoreNonAlpha = stringAlgo.isPalindromeIgnoreNonAlphanumeric(complexString)
      const longestPal = stringAlgo.longestPalindrome(complexString)
      
      return isPalIgnoreCase === false &&
             isPalIgnoreNonAlpha === true &&
             longestPal.length > 0
    })()
  )
}

testPalindrome()
